package com.hxy.manager.activity.goods;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hxy.manager.R;
import com.hxy.manager.activity.CaptureActivity;
import com.hxy.manager.dao.DBManager;
import com.hxy.manager.entity.GoodsEntity;
import com.hxy.manager.entity.GoodsEntityDao;
import com.hxy.manager.service.GoodsEntityService;

/**
 * Created by 傲寒宏志 on 2018/1/1.
 */

public class GoodsShowActivity extends Activity {
    TextView textView0;
    TextView textView1;
    TextView textView2;
    TextView textView3;
    TextView textView4;
    TextView textView5;
    TextView textView6;
    String scanCode;

    public static GoodsShowActivity goodsShowActivity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.show);
        setTitle("商品内容");
        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回


        goodsShowActivity = this;

        textView0 = (TextView) findViewById(R.id.TextView0);
        textView1 = (TextView) findViewById(R.id.TextView1);
        textView2 = (TextView) findViewById(R.id.TextView2);
        textView3 = (TextView) findViewById(R.id.TextView3);

        textView4 = (TextView) findViewById(R.id.TextView4);
        textView5 = (TextView) findViewById(R.id.TextView5);
        textView6 = (TextView) findViewById(R.id.TextView6);

        Intent intent = getIntent();
        scanCode = intent.getStringExtra("scanCode");
        Log.i("kan", "show页面的scanCode：" + scanCode);

        if (scanCode != null) {


            GoodsEntity user = GoodsEntityService.getOneByNumber(GoodsShowActivity.this, scanCode);

            if (user != null) {
                textView0.setText(user.getNumber());
                textView1.setText(user.getName());
                textView2.setText(user.getPrice());
                textView3.setText(user.getPress());
                textView4.setText(user.getDate());
                textView5.setText(user.getAuthor());
                textView6.setText(user.getLocation());
            }
        }


        Button scan = (Button) findViewById(R.id.scan);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //  Intent intent = new Intent(Show.this, CaptureActivity.class);
                //  startActivityForResult(intent , 0x1);
                if (ContextCompat.checkSelfPermission(GoodsShowActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(GoodsShowActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                } else {
                    startActivityForResult(new Intent(GoodsShowActivity.this, CaptureActivity.class), 0);
                }

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.message, menu);

        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:  //默认添加的，左边的TItle向上返回
                finish();
                break;
            /*
            case R.id.message://保存
                Toast.makeText(Show.this,"点击了添加！",Toast.LENGTH_SHORT).show();
                break;
                */
            case R.id.modify:
                Intent intent3 = new Intent(GoodsShowActivity.this, GoodsUpdateActivity.class);
                intent3.putExtra("scanCode", scanCode);
                startActivity(intent3);
        }
        return false;

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent1) {
        super.onActivityResult(requestCode, resultCode, intent1);

        if (resultCode == RESULT_OK && intent1 != null) {
            Bundle bundle = intent1.getExtras();
            //    Toast.makeText(this,"扫描结果："+bundle.getString("result"),Toast.LENGTH_LONG).show();
            //查询数据库
            scanCode = bundle.getString("result");

            GoodsEntityDao goodsEntityDao = DBManager.getReadSession(this).getGoodsEntityDao();
            GoodsEntity user = goodsEntityDao.queryBuilder().where(GoodsEntityDao.Properties.Number.eq(scanCode)).unique();

//            //如果在数据库中不存在，那么就跳转到添加中
            if (user == null) {
                Intent intent = new Intent(GoodsShowActivity.this, GoodsAddActivity.class);
                intent.putExtra("scanCode", scanCode);
                startActivity(intent);
                //跳转并关闭当前activity
                finish();
            } else {


                Intent intent = new Intent(GoodsShowActivity.this, GoodsShowActivity.class);
                intent.putExtra("scanCode", scanCode);
                startActivity(intent);
//                说明查询到了记录
                Toast.makeText(this, "你又扫了一次", Toast.LENGTH_LONG).show();

            }


        } else {

        }
    }

    //bug:删除后直接崩溃，而不是退栈出这个Activity
    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = getIntent();
        String scanCode = intent.getStringExtra("scanCode");
        if (scanCode != null) {



            GoodsEntity user = GoodsEntityService.getOneByNumber(GoodsShowActivity.this, scanCode);

            Log.i("kkk", "检查user是：" + user);
            //不为空就说明这个个是修改后所得到数据
            if (user != null) {

                textView0 = (TextView) findViewById(R.id.TextView0);
                textView1 = (TextView) findViewById(R.id.TextView1);
                textView2 = (TextView) findViewById(R.id.TextView2);
                textView3 = (TextView) findViewById(R.id.TextView3);
                textView4 = (TextView) findViewById(R.id.TextView4);
                textView5 = (TextView) findViewById(R.id.TextView5);
                textView6 = (TextView) findViewById(R.id.TextView6);


                textView0.setText(user.getNumber());
                textView1.setText(user.getName());
                textView2.setText(user.getPrice());
                textView3.setText(user.getPress());
                textView4.setText(user.getDate());
                textView5.setText(user.getAuthor());
                textView6.setText(user.getLocation());
            } else {
                Log.e("kkk", "scanCode为空就说明已经删除了就可以直接关闭当前Activity");
                //空就说明已经删除了就可以直接关闭当前Activity
                finish();
            }


        }

    }

}
